package com.solution.admin.product.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.product.entity.ProductPriceHistoryEntity;
import com.solution.admin.product.service.ProductPriceHistoryService;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 历史价格信息web控制层
 * @author xu_cc
 * @date 2017-12-11 16:51:57
 */
@RequestMapping("/productPriceHistory")
@Controller
public class ProductPriceHistoryController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(ProductPriceHistoryController.class);

	@Autowired
	private ProductPriceHistoryService productPriceHistoryService;
	
	/**
	 * 跳转历史价格信息查询页面
	 * @param ProductPriceHistory 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author xu_cc
	  * @date 2017-12-11 16:51:57
	 */
	@RequestMapping("/toListProductPriceHistoryPage")
	public String toListProductPriceHistoryPage(ProductPriceHistoryEntity productPriceHistory, HttpServletRequest request) {
		return "product/productPriceHistory_list";
	}

	/**
	 * 分页查询历史价格信息列表
	 * @param productPriceHistory 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author xu_cc
	  * @date 2017-12-11 16:51:57
	 */
	@RequestMapping("/listProductPriceHistoryPage")
	@ResponseBody
	public JSONGrid listProductPriceHistoryPage(ProductPriceHistoryEntity productPriceHistory, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			PageBean pageBean = productPriceHistoryService.listProductPriceHistoryPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author xu_cc
	  * @date 2017-12-11 16:51:57
	 */
	@RequestMapping("/toAddProductPriceHistory")
	public String toAddProductPriceHistory(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "product/productPriceHistory_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author xu_cc
	  * @date 2017-12-11 16:51:57
	 */
	@RequestMapping("/toUpdateProductPriceHistory")
	public String toUpdateProductPriceHistory(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			ProductPriceHistoryEntity productPriceHistory = productPriceHistoryService.getProductPriceHistoryById(id);
			request.setAttribute("productPriceHistory", productPriceHistory);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "product/productPriceHistory_form";
	}


	/**
	 * 新增或保存历史价格信息
	 * @param request 请求对象
	 * @param productPriceHistoryEntity 历史价格信息实体
	 * @return
	 * @author xu_cc
	  * @date 2017-12-11 16:51:57
	 */
	@RequestMapping("/saveProductPriceHistory")
	@ResponseBody
	public BaseReturn saveProductPriceHistory(HttpServletRequest request, ProductPriceHistoryEntity productPriceHistoryEntity) {
		BaseReturn result = new BaseReturn();
		try {
			String id = request.getParameter("id");
			productPriceHistoryEntity.setModifier(getUserName());
			if(StringUtil.isNotEmpty(id)){
				productPriceHistoryService.updateProductPriceHistory(productPriceHistoryEntity);
			}else{
				productPriceHistoryEntity.setCreater(getUserName());
				productPriceHistoryService.saveProductPriceHistory(productPriceHistoryEntity);
			}
			result.setCode(SysCode.SUCCESS);
			result.setMsg("历史价格信息保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("历史价格信息保存异常！");
		}
		return result;
	}
	
	
	/**
	 * 修改历史价格信息状态
	 * 
	 * @param request
	 * @param id
	 * @param status
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *			 exception
	 * @Date 创建时间：2017-12-11 16:51:57
	 */
	@RequestMapping("/updateStatus")
	@ResponseBody
	public BaseReturn updateStatus(HttpServletRequest request, Long id, String status) {
		BaseReturn result = new BaseReturn();
		try {
			ProductPriceHistoryEntity productPriceHistoryEntity = new ProductPriceHistoryEntity();
			productPriceHistoryEntity.setId(id);
			productPriceHistoryEntity.setStatus(Integer.parseInt(status));
			productPriceHistoryEntity.setModifier(getUserName());
			int rows = productPriceHistoryService.updateProductPriceHistory(productPriceHistoryEntity);
			if (rows > 0) {
				result.setCode(SysCode.SUCCESS);
				result.setMsg("状态修改成功！");
			} else {
				result.setCode(SysCode.FAILED);
				result.setMsg("状态修改失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("状态更新异常！");
		}
		return result;
	}
}
